package com.ruoyi.system.interAttr.attr;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.outto.core.MapUntils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 密码游戏列表没有接口
 *
 * 等对面开发人员写好   2024 06 27
 *
 */
@Slf4j
@Service
public class CHANGYOUTANGAttr implements AdapterAttr {


    public static void main(String[] args) {
        String wzry_dan = getAtta("sys_wzryDan");
        System.out.println(wzry_dan);
    }

    @Override
    public List<GamesRes> getGames(SalePartner salePartner) {
        Map<String,Object> params = new HashMap<>();
        String url = "http://new.cytds.com/api/getGameList";
        String key = "jianhuncece";
        String secret = "1008a2e7cc233c9f4ea81ee712af0a92";
        params.put("key",salePartner.getSaleKey());
        params.put("secret",salePartner.getOther());
        String options = HttpUtils.doPostFormData(url, params, null);
        String result = MapUntils.unicodeToString(options);
        JSONObject resJson = JSONObject.parseObject(result);
        List<JSONObject> jsonObjects = JSONObject.parseArray(resJson.getString("data"), JSONObject.class);
        List<GamesRes> gamesResList = new ArrayList<>();

        for (JSONObject jsonObject : jsonObjects) {
            GamesRes gamesRes = new GamesRes();
            String id = jsonObject.getString("id");
            String title = jsonObject.getString("name");
            gamesRes.setId(id);
            gamesRes.setGameName(title);
            gamesResList.add(gamesRes);
        }
        return gamesResList;
    }

    @Override
    public List<InterServerRes> getServer(String gameId, SalePartner salePartner) {
        List<InterServerRes> interServerRes = new ArrayList<>();

        return interServerRes;
    }

    @Override
    public List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
        Map<String,Object> params = new HashMap<>();
        String url = "http://new.cytds.com/api/getGameAttrs";
        String key = "jianhuncece";
        String secret = "1008a2e7cc233c9f4ea81ee712af0a92";
        params.put("key",salePartner.getSaleKey());
        params.put("secret",salePartner.getOther());
        params.put("id",gameId);
        String options = HttpUtils.doPostFormData(url, params, null);
        String result = MapUntils.unicodeToString(options);
        JSONObject resJson = JSONObject.parseObject(result);
        List<JSONObject> data = JSONObject.parseArray(resJson.getString("data"), JSONObject.class);
        List<InterAttrRes> interAttrResList = new ArrayList<>();
//        String form_type = jsonObject.getString("form_type");
//        List<JSONObject> data = JSONObject.parseArray(gameFiled1, JSONObject.class);
        List<JSONObject> collect = new ArrayList<>();
        if (Objects.equals("1",type)){
            collect = data.stream().filter(d -> JSONObject.parseObject(d.getString("form_type")).getString("name").equals("单选")).collect(Collectors.toList());
        }else if  (Objects.equals("2",type)){
            collect = data.stream().filter(d -> JSONObject.parseObject(d.getString("form_type")).getString("name").equals("多选")).collect(Collectors.toList());
        }else if (Objects.equals("3",type)){
            collect = data.stream().filter(d -> JSONObject.parseObject(d.getString("form_type")).getString("name").equals("输入框")).collect(Collectors.toList());
        }
        for (JSONObject object : collect) {
            InterAttrRes interAttrRes = new InterAttrRes();
            String id = object.getString("id");
            String name = object.getString("name");
            String childrenValueList = object.getString("children");
            interAttrRes.setId(id);
            interAttrRes.setName(name);
            List<InterAttrRes> interAttrResList1 = new ArrayList<>();
            List<JSONObject> list = JSONObject.parseArray(childrenValueList, JSONObject.class);
            if(!CollectionUtils.isEmpty(list)) {
                for (JSONObject s : list) {
                    InterAttrRes interAttrRes1 = new InterAttrRes();
                    interAttrRes1.setId(s.getString("id"));
                    interAttrRes1.setName(s.getString("name"));
                    interAttrResList1.add(interAttrRes1);
                }
            }
            interAttrRes.setInterServerRes(interAttrResList1);
            interAttrResList.add(interAttrRes);
        }
        return interAttrResList;
    }
    static String getAtta(String fieldDictType) {
        Map<String, Object> param = new HashMap<>();
        String url = "https://www.xizai.com/trade/goodsApi/queryGameField";
        param.put("field_dict_type", fieldDictType);
        String response = HttpUtils.doPostForm(url, param, null);
        return response;
    }
}